package com.qdu.mr.hbase;

import com.qdu.mr.CountRunner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class CountHBaseRunner {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        // 通过HBase的方法创建此配置对象
        // 会额外加载HBase相关的配置文件信息：hbase-site.xml
        Configuration conf = HBaseConfiguration.create();
        conf.set("fs.defaultFS","hdfs://niit-master:9000");
        conf.set("hbase.zookeeper.quorum","niit-master:2181");

        Job job = Job.getInstance(conf);
        job.setJarByClass(CountHBaseRunner.class);

        Scan scan = new Scan();
        scan.addColumn("f".getBytes(),"userid".getBytes());

        TableMapReduceUtil.initTableMapperJob(
                "t_log".getBytes(), // 要查询的表名
                scan,                    // 对表进行过滤的条件
                CountHBaseMapper.class,  // Mapper类
                Text.class,              // Mapper输出的Key的类型
                LongWritable.class,       // Mapper输出的Value的类型
                job,                     // 关联当前方法中的job对象
                true
        );

        job.setReducerClass(CountHBaseReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        FileOutputFormat.setOutputPath(job,new Path("/user/niit/hbaseout/count"));

        job.waitForCompletion(true);
    }
}
